<?php 
/**
*file CatModel.class.php
*栏目类
*
*@auther houtizong
*@version 1.0
*/
defined('ACC') ||exit ('ACC Denied');

class CatModel extends Model{
	protected $table = 'cat'; //
	protected $pk = 'cat_id'; //主键

    //表中的所有字段 跟$_POST过来的字段匹配
	protected $_field = array(
		'cat_id','catname','num'
		);
		
	//自动填充
	protected $_auto = array(
            array('addtime','function','time')
		);
   //验证
   protected $_valid = array(
            array('catname',1,'标题不能为空','require'),
            //array('title',1,'标题已经存在','repeat'),
       );

/**
*查询栏目列名数（catname ）   不能重复
*@param string $catname 列名
*return int 栏目名行数
*/
public function repeat($catname){
	$sql = "select count(*) from ". $this->table . " where catname='". $catname ."'";
	return $this->db->getOne($sql);
}

/**
*查询栏目表所有数据 及art表中 文章的数量
*select cat.cat_id,catname,count(art.cat_id)as num from cat left join art on cat.cat_id=art.cat_id
*where art.cat_id=1;
*@param select语句
*return array
*/
public function artNumber(){
	$sql = "select cat.cat_id,catname,count(art.cat_id) as num from ".$this->table.
	       " left join art on cat.cat_id=art.cat_id ". "group by art.cat_id";
	return $this->db->getAll($sql);
}

/**
*查询cat表catname num=count(art.cat_id)栏目下文章数 首页栏目列表显示
*/
public function catnameNum(){
	$sql = 'select cat.cat_id,catname,count(art.cat_id) as num from '.$this->table.' inner join art on cat.cat_id=art.cat_id group by cat.cat_id';
	return $this->db->getAll($sql);
}

/**
*查询cat表art表 （首页文章所有内容显示）author做user表的时候在加
*分页添加 
*@param $offset
*@param $limit
*/
public function catArtName($offset=0,$limit=5){
	$sql = 'select art_id,title,author,catname,content,FROM_UNIXTIME(pubtime) as pubtime,comm,org_img,thumb_img from '.$this->table.
	' inner join art on cat.cat_id=art.cat_id where is_state=0 order by pubtime desc limit '.$offset.','.$limit;
	return $this->db->getAll($sql);
}

/**
*首页栏目列表 根据$_GET['cat_id'] 查询栏目下所有文章
*@param int $id 
*@param $offset
*@param $limit
*return bool
*/
public function getCatArt($id,$offset=0,$limit=5){
	$sql = 'select art_id,title,author,catname,content,FROM_UNIXTIME(pubtime) as pubtime,comm,org_img,thumb_img from '.$this->table.
	' inner join art on cat.cat_id=art.cat_id '.' where is_state=0 && art.cat_id='.$id.' order by pubtime desc limit '.$offset.','.$limit;
	return $this->db->getAll($sql);

}



}








?>